package com.csf.util;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.DigestUtils;

import java.util.Map;
import java.util.UUID;

public class communityUtils {
//    生成随机字符串
    public static String getRandom(){
        return UUID.randomUUID().toString().replaceAll("-","");
    }
//    密码加密  MD5加密
    public static String mdFive(String key){
          if (StringUtils.isBlank(key)) {
              return null;
          }
          return DigestUtils.md5DigestAsHex(key.getBytes());
    }

//    json转换   code 返回数字决定运行状态   msg运行状态信息   map可能会往
    public static String getJsonString(int code, String msg, Map<String,Object> map){

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code",code);
        jsonObject.put("msg",msg);
        if (map != null) {
            for (String key : map.keySet()) {
                jsonObject.put(key, map.get(key));
            }
        }
        return jsonObject.toJSONString();
    }

    public static String getJsonString(int code,String msg){
        return getJsonString(code, msg,null);
    }

    public static String getJsonString(int code){
        return getJsonString(code,null,null);
    }
}
